<?php

namespace app\admin\model\user;

use think\Model;


class Userfinancial extends Model
{

    

    

    // 表名
    protected $name = 'user_financial';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'day_time_text',
        'remaining_text',
        'createtime_text'
    ];



    public function getStatusList()
    {
        return ['1' => __('Status 1'), '0' => __('Status 0'),'2' => __('Status 2')];
    }

    public function getRemainingTextAttr($value,$data)
    {
        if(isset($data['day_time'])){
            if(time() < $data['day_time']){
                $value = abs(($data['day_time'] - time()));
            }
        }
        return $value;
    }

    public function getCreatetimeTextAttr($value, $data)
    {
        if (isset($data['createtime'])) {
            $value = date("Y-m-d H:i:s", $data['createtime']);
        }
        return $value;
    }


    public function getDayTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['day_time']) ? $data['day_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setDayTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }




    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function financial()
    {
        return $this->belongsTo('app\admin\model\Financial', 'financial_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
